package com.factory.ssm.handler;


import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.factory.ssm.beans.Factory;
import com.factory.ssm.beans.Pharmaceutical;
import com.factory.ssm.service.FactoryService;
import com.factory.ssm.service.PharmaceuticalService;

/**

 *
 * @author testjava
 * @since 2020-08-03
 */
@Controller
@RequestMapping("/pharmaceutical")
public class PharmaceuticalController {
	
	@Autowired
	PharmaceuticalService pharmaceuticalService;
	
	@Autowired
	FactoryService factoryService;
	
	@RequestMapping(value = "/getPharmaceutical/{factoryId}",method = RequestMethod.GET)
	public String getPharmaceuticalList(@PathVariable String factoryId,Model model) {
		System.out.println(factoryId);
		List<Pharmaceutical> pharmaceuticalList = pharmaceuticalService.getpharmaceuticalByFactoryId(factoryId);
		model.addAttribute("pharmaceuticalList", pharmaceuticalList);
		model.addAttribute("factoryId", factoryId);
		System.out.println(pharmaceuticalList.toString());
		return "pharmaceuticalIndex";
	}
	
	@RequestMapping(value = "/showAddPharmaceutical/{factoryId}")
	public String showAddPharmaceutical(@PathVariable Integer factoryId,Model model) {
		System.out.println(factoryId);
		model.addAttribute("factoryId", factoryId);
		
		return "addPharmaceutical";
	}
	
	@RequestMapping(value = "/delete/{id}/{factoryId}",method = RequestMethod.GET)
	public String deletePharmaceutical(@PathVariable Integer id,@PathVariable Integer factoryId) {
		System.out.println(id);
		pharmaceuticalService.deletePharmaceuticalById(id);
		return "redirect:/pharmaceutical/getPharmaceutical/{factoryId}";
	}
	
	
	@RequestMapping(value = "/addPharmaceutical/{factoryId}")
	public String addPharmaceutical(@PathVariable Integer factoryId,HttpServletRequest req,Model model,Pharmaceutical pharmaceutical,MultipartFile file) {
		
		String fileName = file.getOriginalFilename();
		ServletContext applicationContext = req.getSession().getServletContext();
		String imgSavePath = applicationContext.getRealPath("img");
		String uuid = UUID.randomUUID().toString();
		String suffix = fileName.substring(fileName.lastIndexOf("."));
		String targetFileName = uuid +suffix;
 		File targetFile = new File(imgSavePath+"/"+targetFileName);
 		
		try {
			file.transferTo(targetFile);
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	
		pharmaceutical.setCover(targetFileName);
		pharmaceutical.setGmtCreate(new Date().toString());
		pharmaceutical.setGmtModified(new Date().toString());
		pharmaceutical.setIsDeleted("0");
		
		Factory factory = factoryService.getFactoryById(factoryId);
		pharmaceutical.setFactoryId(factoryId);
		pharmaceutical.setFactoryName(factory.getName());
		pharmaceutical.setFactoryAddress(factory.getAddress());	
		pharmaceutical.setFactoryPhone(factory.getPhone());
		System.out.println(pharmaceutical);
		
		pharmaceuticalService.insert(pharmaceutical);
		
		
	
		model.addAttribute("factoryId", factoryId);
		
		return "redirect:/pharmaceutical/getPharmaceutical/{factoryId}";
	}
	
	@RequestMapping(value = "/showEdit/{id}/{factoryId}")
	public String showEditPharmaceutical(@PathVariable Integer id,@PathVariable String factoryId,Model model) {
		Pharmaceutical pharmaceutical = pharmaceuticalService.getPharmaceuticalById(id);
		model.addAttribute("id", id);
		model.addAttribute("factoryId", factoryId);
		model.addAttribute("pharmaceutical", pharmaceutical);
		return "addPharmaceutical";
	}
	
	@RequestMapping(value = "/edit/{id}/{factoryId}")
	public String editPharmaceutical(Pharmaceutical pharmaceutical,@PathVariable Integer id,@PathVariable Integer factoryId,Model model) {
		System.out.println("xiugai");
		pharmaceutical.setId(id);
		System.out.println(pharmaceutical);
		pharmaceuticalService.updatePharmaceuticalById(pharmaceutical);
		
		return "redirect:/pharmaceutical/getPharmaceutical/{factoryId}";
	}


	
	
	

}

